package com.taiyi.renaissancemall.ui.mine.wallet.withdraw;

import android.content.Intent;
import android.util.Log;

import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.Utils.Utils;
import com.taiyi.renaissancemall.base.BasePresent;
import com.taiyi.renaissancemall.base.MyApp;
import com.taiyi.renaissancemall.bean.BaseResultBean;
import com.taiyi.renaissancemall.bean.GoTakeMoneyBean;
import com.taiyi.renaissancemall.bean.ReturnMoneyBean;
import com.taiyi.renaissancemall.bean.VerifiedBean;
import com.taiyi.renaissancemall.net.BaseUrl;
import com.taiyi.renaissancemall.net.HttpUtils;
import com.taiyi.renaissancemall.net.callbck.JsonCallback;
import com.taiyi.renaissancemall.ui.login.LoginActivity;
import com.taiyi.renaissancemall.ui.mine.wallet.transfer.TransferView;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Response;

public class WithdrawPresent extends BasePresent<WithdrawView> {

    //去用户提现页面
    public void getWithdrawDetail() {
        Map<String, String> map = new HashMap<>();
        HttpUtils.postRequest(BaseUrl.GO_RETURN_MONEY, this, map, new JsonCallback<BaseResultBean<ReturnMoneyBean>>() {
            @Override
            public BaseResultBean<ReturnMoneyBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<ReturnMoneyBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.getWithdrawSuccess(response.body().data);
                } else if (response.body().code == -1) {
                    ToastUtils.showShortToast(response.body().msg_cn);
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.getWithdrawFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<ReturnMoneyBean>> response) {
                super.onError(response);
            }
        });
    }

    //提现
    public void toWithdraw(String bindingid, String money) {
        Map<String, String> map = new HashMap<>();
        map.put("bindingid", bindingid);
        map.put("money", money);
        HttpUtils.postRequest(BaseUrl.RETURN_MONEY, this, map, new JsonCallback<BaseResultBean<VerifiedBean>>() {
            @Override
            public BaseResultBean<VerifiedBean> convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<BaseResultBean<VerifiedBean>> response) {
                super.onSuccess(response);
                if (response.body().code == 0) {
                    view.toWithdrawSuccess(response.body().msg_cn);
                } else if (response.body().code == -1) {
                    ToastUtils.showShortToast(response.body().msg_cn);
                    Utils.getSpUtils().remove("token");
                    Intent intent = new Intent(MyApp.getInstance(), LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    MyApp.getInstance().startActivity(intent);
                } else {
                    view.toWithdrawFailure(response.body().msg_cn);
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<BaseResultBean<VerifiedBean>> response) {
                super.onError(response);
            }
        });
    }

    public void getUserInfo(String accessToken, String openId) {
        Map<String, String> map = new HashMap<>();
        String getUserInfo = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openId;
        HttpUtils.postRequest(getUserInfo, this, map, new JsonCallback<String>() {
            @Override
            public String convertResponse(Response response) throws Throwable {
                return super.convertResponse(response);
            }

            @Override
            public void onSuccess(com.lzy.okgo.model.Response<String> response) {
                super.onSuccess(response);
                String nickName = null;
                try {
                    JSONObject jsonObject = new JSONObject(response.body());
                    nickName = jsonObject.getString("nickname");
                    view.getNickName(nickName);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(com.lzy.okgo.model.Response<String> response) {
                super.onError(response);
            }
        });
    }

}
